package top.tocchen.bookshop.controller;

import top.tocchen.bookshop.entity.CartEntity;
import top.tocchen.bookshop.entity.OrderEntity;
import top.tocchen.bookshop.entity.OrderDoEntity;
import top.tocchen.bookshop.service.CartService;
import top.tocchen.bookshop.service.OrderService;
import top.tocchen.bookshop.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author tocchen
 * @date 2022/8/9 19:34
 * @since jdk 1.8
 **/
@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;
    @Autowired
    SearchService searchService;
    @RequestMapping("/order")
    public String goOrder(ModelMap map, HttpSession session){
        if(session.getAttribute("user_id")==null){
            return "login";
        }
        int userId = (int)session.getAttribute("user_id");
        List<OrderDoEntity> orderList = orderService.findAll(userId);
        for (OrderDoEntity d:orderList
             ) {
            System.out.println(d.toString());
        }
        map.addAttribute("value",orderList);
        return "order";
    }
    @RequestMapping("/order/add")
    @ResponseBody
    public String addOrder(HttpSession session){
        OrderEntity order = new OrderEntity();
        order.setUserId((int)session.getAttribute("user_id"));
        List<CartEntity> cartlist = cartService.findAll();
        int num = 0;
        for (CartEntity cart: cartlist
             ) {
            System.out.println(cart);
            order.setCreateDate(new Date());
            order.setBookNum(cart.getCount());
            order.setBookId(cart.getBid());
            System.out.println(order.toString());
            orderService.insert(order);
        }

        return "300";
    }
}


